Repository navigation
feat: add event deferral to states - #664
Open
SahilRajputdotexe wants to merge 1 commit into
Open
SahilRajputdotexe wants to merge 1 commit into
SahilRajputdotexe wants to merge 1 commit into
Conversation
A state can list events to defer with State(defer=[...]), and compound states defer for their descendants. An event that no transition handles and that every active atomic state defers is queued instead of dropped, and sent again, in order, once the configuration stops deferring it. Adds on_defer/on_replay callbacks, the deferred_events, pending_deferred_event_ids, is_event_deferred() and clear_deferred_queue() introspection API on StateChart, and an opt-in validate_deferred_events check. Both the sync and async engines support it. Signed-off-by: Sahil Rajput <90515841+SahilRajputdotexe@users.noreply.github.com>
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This adds UML-style event deferral. A state can list events it wants to hold back with
State(defer=[...]). If an event arrives, no transition handles it, and every active atomic state defers it, the engine queues it instead of dropping it (or raisingTransitionNotAllowed). Once the machine reaches a configuration that no longer defers the event, the queued events go back to the external queue in the order they were sent, with their original arguments.It is useful when an event can legitimately show up before the machine is ready for it, so callers don't have to buffer it themselves.
Details:
class mordor(State.Compound, defer=["rest"])). In parallel states the event is only deferred if all regions defer it.on_defer/on_replaycallbacks, inline or by naming convention (on_defer_event,on_defer_<state>,on_replay_event,on_replay_<state>).StateChartgetsdeferred_events,pending_deferred_event_ids,is_event_deferred()andclear_deferred_queue().validate_deferred_events = True(off by default) checks the ids against the declared events.I added a docs page (
docs/deferral.md), entries in the states and validations pages, and a 3.3.0 release notes file since this is a new feature and 3.2.2 is a bug fix release. Happy to move the notes if you prefer another place.Tests are in
tests/test_statechart_deferral.py, most of them run on both engines throughsm_runner. The full suite passes with 100% coverage, and ruff and mypy are clean. Lint and tests pass on my fork for Python 3.10 to 3.14.